package com.vpen.clib.result;


import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * @author yst
 */
@Data
public class CResult<T> implements Serializable {
    private static final long serialVersionUID = 456798615676372L;
    
    private String traceId;
    private int code;
    private String msg;
    private T data;

    /***构造器***/
    private CResult() {
        this(CODE_MSG.SUCCESS, null);
    }

    private CResult(CODE_MSG codeMsg) {
        this(codeMsg, null);
    }

    private CResult(T data) {
        this(CODE_MSG.SUCCESS, data);
    }

    private CResult(CODE_MSG codeMsg, T data) {
        this(codeMsg.getCode(), codeMsg.getMsg(), data);
    }

    private CResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.traceId = MDC.get("traceId");
    }

    /***成功的响应***/
    public static <T> CResult<T> success() {
        return new CResult<>();
    }

    public static <T> CResult<T> success(CODE_MSG codeMsg) {
        return new CResult<>(codeMsg);
    }

    public static <T> CResult<T> success(T data) {
        return new CResult<>(data);
    }

    public static <T> CResult<T> success(int code, String msg, T data) {
        return new CResult<T>(code, msg, data);
    }

    /***失败的相应***/
    public static <T> CResult<T> error() {
        return new CResult<>(CODE_MSG.ERROR);
    }

    public static <T> CResult<T> error(T data) {
        return new CResult<>(CODE_MSG.ERROR, data);
    }

    public static <T> CResult<T> error(CODE_MSG codeMsg) {
        return new CResult<>(codeMsg);
    }

    public static <T> CResult<T> autoCResult(Boolean success) {
        return success ? success() : error();
    }

    /***其他响应***/
    public static <T> CResult<T> otherCResult(CODE_MSG codeMsg, T data) {
        return new CResult<>(codeMsg, data);
    }

    public static <T> CResult<T> autoCResult(Boolean success, T data) {
        return success ? success(data) : error(data);
    }


}
